define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/room/index',
                    add_url: 'order/room/add',
                    edit_url: 'order/room/edit',
                    del_url: 'order/room/del',
                    multi_url: 'order/room/multi',
                    table: 'shop_task_key',
                    ajax_url: {
                        btn_1: "order/room/edit"//详情
                    },
                    btn_mall_window:{
                        btn_array:["立即提交","取消"],
                        btn_evn_array_length:2,
                        btn_evn_array: {
                            evn_1:function (index,layero) {
                                var frame = Layer.getChildFrame('html', index);
                                var form = $("form",frame);
                                //form.append(" <input name=\"type\"  value=\"1\" style=\"display: none\">");
                                var success = function (data,ret) {
                                    console.log(data,ret);
                                    Layer.msg(data.responseJSON.msg);
                                    table.bootstrapTable('refresh');
                                    Layer.close(index);
                                }
                                Fast.api.form_ajax_submit(form,success);
                            },
                            evn_2:function (index,layero) {
                                Layer.close(index);
                            }
                        },
                        area:['800px','600px']
                    }
                    // div_evn:{
                    //     btn_1:{
                    //         divfunction:function(row){
                    //             var data = table.bootstrapTable('getSelections');
                    //             var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                    //             layer.confirm('确定发布？',{
                    //                 btn: ['确定','取消'] //可以无限个按钮
                    //                 },function(index, layero){
                    //                     Fast.api.ajax({
                    //                         url: 'shop/room/update_status',
                    //                         data: {
                    //                          'row[ids]': row['task_id'],
                    //                         },
                    //                         success: function (data) {
                    //                            layer.msg(data.msg);
                    //                         },
                    //                         error: function (data) {
                    //                            //失败的回调
                    //                           layer.msg(data.msg);
                    //                         }
                    //                     });
                    //                 },function(index, layero){
                    //                     layer.msg('已取消操作。');
                    //                 }
                    //             );
                    //         }
                    //     }
                    // },
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'take_key_id',
                sortName: 'take_key_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'task_sno', title: __('Task_sno')},
                        {field: 'goods_sno', title: __('Good_name')},
                        {field: 'key_word', title: __('Keyword')},
                        {field: 'service_change_price', title: __('客服改价'), operate:'BETWEEN',operate:false},
                        {field: 'beat_price', title: __('拍下价格'), operate:'BETWEEN',operate:false},
                        {field: 'left_num', title: __('Task_num')},
                        {field: 'left_task_num_one', title: __('Left_task_num')},
                        {field: 'left_task_num_tow', title: __('Left_task_num_tow'), operate:'BETWEEN',operate:false},
                        {field: 'left_task_num_tow', title: __('Left_task_num_tow'), operate:'BETWEEN',operate:false},
                        {field: 'create_time', title: __('房间创建时间'),operate:'BETWEEN', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,

                            events: Table.api.events.operate, formatter: Table.api.formatter.operate,formatter: function (value, row, index) {
                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);

                                buttons.push(
                                {
                                    name: 'btn_1',
                                    text: "编辑",
                                    extend: 'data-toggle="tooltip" data-name="btn_1" data-title="编辑"',
                                    classname: 'btn btn-xs btn-success btn-magic btn-small-window'
                                })

                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                            }}
                    ]
                ]
            });


            $(document).on("click", "#sendTask", function (e){
                var data = table.bootstrapTable('getSelections');
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定发布？',{
                    btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'shop/task/lists/update_status',
                            data: {
                             'ids': data,

                            },
                            success: function (data) {
                               layer.msg(data.msg);
                            },
                            error: function (data) {
                               //失败的回调
                              layer.msg(data.msg);
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });

            $(document).on("click", "#add", function (e){
                var data = table.bootstrapTable('getSelections');
                var post_data = JSON.stringify(data);
                var data = table.bootstrapTable('getSelections');
                var post_data = JSON.stringify(data);
                var btn_array = ['确定', '保存草稿'];
                var btn_evn_array = {
                    evn_1:function (index,layero) {
                         var frame = Layer.getChildFrame('html', index);
                         $("#type",frame).val('send');

                        var form = $("form",frame);
                        var success = function (data, ret) {
                            var data = data.responseJSON;
                            if(data.code == 1){
                                layer.msg(data.msg);
                                table.bootstrapTable('refresh');
                                Layer.close(index);
                            }else{
                                layer.alert(data.msg)
                            }
                        }
                        Fast.api.form_ajax_submit(form,success);
                         return false;
                    },
                    evn_2:function (index,layero) {
                        var frame = Layer.getChildFrame('html', index);
                        console.log(2);
                        $("#type", frame).val('draft');
                        var form = $("form",frame);
                        var success = function (data, ret) {
                            var data = data.responseJSON;
                            //return;
                            if(data.code == 1){
                                layer.msg(data.msg);
                                table.bootstrapTable('refresh');
                                Layer.close(index);
                            }else{

                                layer.alert(data.msg);
                            }
                        }
                        Fast.api.form_ajax_submit(form,success);
                        return false;
                    }
                };
                Fast.api.new_open("shop/task/lists/add?data="+post_data, __('添加任务'),btn_array,btn_evn_array,2);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // $(document).on("click","#draft",function(){
            //     $("#type").val('draft');
            //     $("#add_form").submit();
            // });
            // $(document).on("click","#ok",function(){
            //     $("#type").val('send');
            //     $("#add_form").submit();
            // });

            $(document).on("click","#next_1",function(){
                $("#shop_choose").removeClass('active');
                $("#tab_1").removeClass('active');
                $("#module").addClass('active');
                $("#tab_2").addClass('active');
            });
            $(document).on("click","#last_1",function(){
                $("#shop_choose").addClass('active');
                $("#tab_1").addClass('active');
                $("#module").removeClass('active');
                $("#tab_2").removeClass('active');
            });

            $(document).on("click","#next_2",function(){
                $("#module").removeClass('active');
                $("#tab_2").removeClass('active');
                $("#others").addClass('active');
                $("#tab_3").addClass('active');
            });

            $(document).on("click","#last_2",function(){
                $("#module").addClass('active');
                $("#tab_2").addClass('active');
                $("#others").removeClass('active');
                $("#tab_3").removeClass('active');
            });

            $(document).on("click","#ok",function(){
                $("#type").val('send');
                $("#add-form").submit();
            });

            var html = '';
            $(document).on("click","#add_key",function(){
                var len = $("#key_word").children().length;
                //console.log(len)
                if(len > 10){
                    alert('最多只能添加十个关键字');
                    return false;
                }
                len++;
                html = '<div id="key_'+len+'">\
                            <div class="col-xs-12 col-sm-6">\
                                <div class="form-group">\
                                    <label class="control-label col-xs-12 col-sm-4"></label>\
                                    <div class="col-xs-12 col-sm-4">\
                                        <input id="" data-rule="required" class="form-control form-control" name="row[key_word][]" type="text">\
                                    </div>\
                                </div>\
                            </div>\
                            <div class="col-xs-12 col-sm-6">\
                                <div class="form-group">\
                                    <label class="control-label col-xs-12 col-sm-4"></label>\
                                    <div class="col-xs-12 col-sm-4">\
                                        <input id="" data-rule="required" class="form-control form-control" name="row[task_num][]" type="num">\
                                    </div>\
                                </div>\
                            </div>\
                        </div>';
                $("#key_word").append(html)
            });

            $(document).on("click","#del_key",function(){
                var len = $("#key_word").children().length;
                if(len < 2){
                    alert('至少要保留一个关键字');
                    return false;
                }
                $("#key_"+len).remove();
                len--;
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});